package com.yyfax.ph.home.todolist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.yyfax.common.recyclerview.Divider;
import com.yyfax.common.recyclerview.OnItemClickListener;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.R;
import com.yyfax.ph.base.RecyclerListActivity;
import com.yyfax.ph.common.utils.Constants;
import com.yyfax.ph.progress.detail.ProgressDetailActivity;
import com.yyfax.ph.progress.search.ProgressModel;
import com.yyfax.ph.progress.search.ProgressPresenter;

import java.util.List;

/**
 * 待办事项列表
 * Created by ying_he on 2017/10/31.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class ToDoListActivity extends RecyclerListActivity implements ProgressPresenter.View, OnItemClickListener{
	private static final String TAG = ToDoListActivity.class.getSimpleName();
	private static final int LOADER_ID_TODO_LIST = 1001;

	public static final int FIRST_PAGE = 1;

	private ToDoListAdapter mToDoListAdapter;
	private ProgressPresenter mToDoListPresenter;

	private static String mToDoListSize = "";
	private int mPageNum = 1;
	private int mPageSize = 20;

	public static void start(Activity aty, String todoListSize) {
		mToDoListSize = todoListSize;
		Intent intent = new Intent(aty, ToDoListActivity.class);
		aty.startActivity(intent);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.activity_todo_list;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopBarVisible(true);
		setBtnBackBackground(R.drawable.ic_black_back);
		setTitle(R.string.title_todo_list);
		mToDoListAdapter = new ToDoListAdapter();
		mToDoListAdapter.setOnItemClickListener(this);
		setup(this, R.id.todo_list_rv, 1, mToDoListAdapter);
		mRecyclerView.removeItemDecoration(mDivider);
		mDivider = new Divider(DensityUtil.dip2px(this, 0.5f), getResources().getColor(R.color.color_efefef),
				false, 0, 0, 0, 0, 0);
		mRecyclerView.addItemDecoration(mDivider);
		mRecyclerView.setRefreshHeaderBgColor(getResources().getColor(R.color.color_white));
		getSupportLoaderManager().initLoader(LOADER_ID_TODO_LIST, null, this);
	}

	@Override
	public void setDefaultDataErrorLayout() {
		mNoNetworkLayout = getLayoutInflater().inflate(R.layout.state_view_no_network, mRecyclerView, false);
		mEmptyLayout = getLayoutInflater().inflate(R.layout.state_view_no_data, mRecyclerView, false);
		ImageView noDataIv = mEmptyLayout.findViewById(R.id.no_data_iv);
		TextView noDataTv = mEmptyLayout.findViewById(R.id.no_data_tv);
		noDataIv.setImageResource(R.drawable.no_data_icon_progress);
		noDataTv.setText(R.string.no_todo_list);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new ProgressPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mToDoListPresenter = (ProgressPresenter) data;
		mToDoListPresenter.attach(this);
		requestData(true);
	}

	@Override
	public void onItemClick(int position, Object obj, View v) {
		ProgressModel.ProgressBean progressBean = (ProgressModel.ProgressBean) obj;
		ProgressDetailActivity.start(this, progressBean.taskNo);
	}

	private void requestData(boolean isRefresh){
		if (isRefresh){
			mPageNum = FIRST_PAGE;
		}else {
			++ mPageNum;
		}
		mToDoListPresenter.getProgress("", Constants.PROGRESS_STATUS_WAITE_DEAL, mPageNum, mPageSize);
	}

	@Override
	public void onRefresh() {
		requestData(true);
	}

	@Override
	public void onLoadMore() {
		requestData(false);
	}

	@Override
	public void refreshSuccess(List<ProgressModel.ProgressBean> list) {
		mRecyclerView.stopRefresh();
		mStatesAdapter.refreshData(list);

		if (list != null && list.size() > 0){
			mRecyclerView.setLoadMoreEnabled(true);
		}else {
			mRecyclerView.setLoadMoreEnabled(false);
		}
	}

	@Override
	public void loadMoreSuccess(List<ProgressModel.ProgressBean> list) {
		mRecyclerView.stopLoading();
		mStatesAdapter.loadData(list);
	}

	@Override
	public void onGetProgressFailure(String tips) {
		mPageNum --;
		mRecyclerView.setRefreshing(false);
		mRecyclerView.stopLoading();
		if (mPageNum <= 1) {
			mStatesAdapter.setState(com.yyfax.common.adapter.StatesAdapter.STATE_ERROR);
		}
	}


	@Override
	public void onRetry() {
		mRecyclerView.setRefreshing(true);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID_TODO_LIST);
	}
}
